$(document).ready( function() {
	var allChildren = $('body').children();
	var ckWidth = $('#previewId').attr('ckWidth');
	var newWrap = $('<div/>').attr('id','outerWrap').css('width',ckWidth+'px').css('margin','auto');
	allChildren.wrapAll(newWrap);
	
	var sessionStorage = window.sessionStorage;
	var tabIds = new Array();
	$('.smarttable-preview').each(function(index){
		var $_table = $(this).children();
		var tabId = $_table.attr('id');
		if(tabId){
			$_table.remove();
			
			tabId = tabId.replace('_wrapper','');
			tabIds[index] = tabId;
			
			var tableHtml = sessionStorage.getItem(tabId);
			
			$(this).append(tableHtml);
		}
	});
	
	
	$('.smart_chart').each(function(){
		var $_self = $(this);
		var newDiv = $('<div/>').attr('style','width:100%;height:420px');
		$_self.after(newDiv);
		
		var chartId = $_self.data('chartid');
		var chartOption = sessionStorage.getItem(chartId);
		if(chartOption){
		   	var chart = echarts.init(newDiv[0]);
			chart.setOption(JSON.parse(chartOption));
		}
		
		$_self.remove();
	});
	
	
	for(var i=0;i<tabIds.length;i++){
		var tabId = tabIds[i];
		var width = sessionStorage.getItem(tabId+'_width');
		ck.util.builTableData(tabIds[i],width);
	}
});